home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / var / lib / python-support / python2.6 / dbus / bus.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  15.2 KB  |  358 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __all__ = ('BusConnection',)
  5. __docformat__ = 'reStructuredText'
  6. import logging
  7. import weakref
  8. from _dbus_bindings import validate_interface_name, validate_member_name, validate_bus_name, validate_object_path, validate_error_name, BUS_SESSION, BUS_STARTER, BUS_SYSTEM, DBUS_START_REPLY_SUCCESS, DBUS_START_REPLY_ALREADY_RUNNING, BUS_DAEMON_NAME, BUS_DAEMON_PATH, BUS_DAEMON_IFACE, NAME_FLAG_ALLOW_REPLACEMENT, NAME_FLAG_DO_NOT_QUEUE, NAME_FLAG_REPLACE_EXISTING, RELEASE_NAME_REPLY_NON_EXISTENT, RELEASE_NAME_REPLY_NOT_OWNER, RELEASE_NAME_REPLY_RELEASED, REQUEST_NAME_REPLY_ALREADY_OWNER, REQUEST_NAME_REPLY_EXISTS, REQUEST_NAME_REPLY_IN_QUEUE, REQUEST_NAME_REPLY_PRIMARY_OWNER
  9. from dbus.connection import Connection
  10. from dbus.exceptions import DBusException
  11. from dbus.lowlevel import HANDLER_RESULT_NOT_YET_HANDLED
  12. _NAME_OWNER_CHANGE_MATCH = "type='signal',sender='%s',interface='%s',member='NameOwnerChanged',path='%s',arg0='%%s'" % (BUS_DAEMON_NAME, BUS_DAEMON_IFACE, BUS_DAEMON_PATH)
  13. _NAME_HAS_NO_OWNER = 'org.freedesktop.DBus.Error.NameHasNoOwner'
  14. _logger = logging.getLogger('dbus.bus')
  15.  
  16. class NameOwnerWatch(object):
  17.     __slots__ = ('_match', '_pending_call')
  18.     
  19.     def __init__(self, bus_conn, bus_name, callback):
  20.         validate_bus_name(bus_name)
  21.         
  22.         def signal_cb(owned, old_owner, new_owner):
  23.             callback(new_owner)
  24.  
  25.         
  26.         def error_cb(e):
  27.             if e.get_dbus_name() == _NAME_HAS_NO_OWNER:
  28.                 callback('')
  29.             else:
  30.                 logging.basicConfig()
  31.                 _logger.debug('GetNameOwner(%s) failed:', bus_name, exc_info = (e.__class__, e, None))
  32.  
  33.         self._match = bus_conn.add_signal_receiver(signal_cb, 'NameOwnerChanged', BUS_DAEMON_IFACE, BUS_DAEMON_NAME, BUS_DAEMON_PATH, arg0 = bus_name)
  34.         self._pending_call = bus_conn.call_async(BUS_DAEMON_NAME, BUS_DAEMON_PATH, BUS_DAEMON_IFACE, 'GetNameOwner', 's', (bus_name,), callback, error_cb, utf8_strings = True)
  35.  
  36.     
  37.     def cancel(self):
  38.         if self._match is not None:
  39.             self._match.remove()
  40.         
  41.         if self._pending_call is not None:
  42.             self._pending_call.cancel()
  43.         
  44.         self._match = None
  45.         self._pending_call = None
  46.  
  47.  
  48.  
  49. class BusConnection(Connection):
  50.     '''A connection to a D-Bus daemon that implements the
  51.     ``org.freedesktop.DBus`` pseudo-service.
  52.  
  53.     :Since: 0.81.0
  54.     '''
  55.     TYPE_SESSION = BUS_SESSION
  56.     TYPE_SYSTEM = BUS_SYSTEM
  57.     TYPE_STARTER = BUS_STARTER
  58.     START_REPLY_SUCCESS = DBUS_START_REPLY_SUCCESS
  59.     START_REPLY_ALREADY_RUNNING = DBUS_START_REPLY_ALREADY_RUNNING
  60.     
  61.     def __new__(cls, address_or_type = TYPE_SESSION, mainloop = None):
  62.         bus = cls._new_for_bus(address_or_type, mainloop = mainloop)
  63.         bus._bus_names = weakref.WeakValueDictionary()
  64.         bus._signal_sender_matches = { }
  65.         return bus
  66.  
  67.     
  68.     def add_signal_receiver(self, handler_function, signal_name = None, dbus_interface = None, bus_name = None, path = None, **keywords):
  69.         named_service = keywords.pop('named_service', None)
  70.         if named_service is not None:
  71.             if bus_name is not None:
  72.                 raise TypeError('bus_name and named_service cannot both be specified')
  73.             bus_name is not None
  74.             bus_name = named_service
  75.             warn = warn
  76.             import warnings
  77.             warn('Passing the named_service parameter to add_signal_receiver by name is deprecated: please use positional parameters', DeprecationWarning, stacklevel = 2)
  78.         
  79.         match = super(BusConnection, self).add_signal_receiver(handler_function, signal_name, dbus_interface, bus_name, path, **keywords)
  80.         if bus_name is not None and bus_name != BUS_DAEMON_NAME:
  81.             if bus_name[:1] == ':':
  82.                 
  83.                 def callback(new_owner):
  84.                     if new_owner == '':
  85.                         match.remove()
  86.                     
  87.  
  88.             else:
  89.                 callback = match.set_sender_name_owner
  90.             watch = self.watch_name_owner(bus_name, callback)
  91.             self._signal_sender_matches[match] = watch
  92.         
  93.         self.add_match_string(str(match))
  94.         return match
  95.  
  96.     
  97.     def _clean_up_signal_match(self, match):
  98.         self.remove_match_string_non_blocking(str(match))
  99.         watch = self._signal_sender_matches.pop(match, None)
  100.         if watch is not None:
  101.             watch.cancel()
  102.         
  103.  
  104.     
  105.     def activate_name_owner(self, bus_name):
  106.         if bus_name is not None and bus_name[:1] != ':' and bus_name != BUS_DAEMON_NAME:
  107.             
  108.             try:
  109.                 return self.get_name_owner(bus_name)
  110.             except DBusException:
  111.                 e = None
  112.                 if e.get_dbus_name() != _NAME_HAS_NO_OWNER:
  113.                     raise 
  114.                 e.get_dbus_name() != _NAME_HAS_NO_OWNER
  115.                 self.start_service_by_name(bus_name)
  116.                 return self.get_name_owner(bus_name)
  117.             
  118.  
  119.         None<EXCEPTION MATCH>DBusException
  120.         return bus_name
  121.  
  122.     
  123.     def get_object(self, bus_name, object_path, introspect = True, follow_name_owner_changes = False, **kwargs):
  124.         '''Return a local proxy for the given remote object.
  125.  
  126.         Method calls on the proxy are translated into method calls on the
  127.         remote object.
  128.  
  129.         :Parameters:
  130.             `bus_name` : str
  131.                 A bus name (either the unique name or a well-known name)
  132.                 of the application owning the object. The keyword argument
  133.                 named_service is a deprecated alias for this.
  134.             `object_path` : str
  135.                 The object path of the desired object
  136.             `introspect` : bool
  137.                 If true (default), attempt to introspect the remote
  138.                 object to find out supported methods and their signatures
  139.             `follow_name_owner_changes` : bool
  140.                 If the object path is a well-known name and this parameter
  141.                 is false (default), resolve the well-known name to the unique
  142.                 name of its current owner and bind to that instead; if the
  143.                 ownership of the well-known name changes in future,
  144.                 keep communicating with the original owner.
  145.                 This is necessary if the D-Bus API used is stateful.
  146.  
  147.                 If the object path is a well-known name and this parameter
  148.                 is true, whenever the well-known name changes ownership in
  149.                 future, bind to the new owner, if any.
  150.  
  151.                 If the given object path is a unique name, this parameter
  152.                 has no effect.
  153.  
  154.         :Returns: a `dbus.proxies.ProxyObject`
  155.         :Raises `DBusException`: if resolving the well-known name to a
  156.             unique name fails
  157.         '''
  158.         if follow_name_owner_changes:
  159.             self._require_main_loop()
  160.         
  161.         named_service = kwargs.pop('named_service', None)
  162.         if named_service is not None:
  163.             if bus_name is not None:
  164.                 raise TypeError('bus_name and named_service cannot both be specified')
  165.             bus_name is not None
  166.             warn = warn
  167.             import warnings
  168.             warn('Passing the named_service parameter to get_object by name is deprecated: please use positional parameters', DeprecationWarning, stacklevel = 2)
  169.             bus_name = named_service
  170.         
  171.         if kwargs:
  172.             raise TypeError('get_object does not take these keyword arguments: %s' % ', '.join(kwargs.iterkeys()))
  173.         kwargs
  174.         return self.ProxyObjectClass(self, bus_name, object_path, introspect = introspect, follow_name_owner_changes = follow_name_owner_changes)
  175.  
  176.     
  177.     def get_unix_user(self, bus_name):
  178.         '''Get the numeric uid of the process owning the given bus name.
  179.  
  180.         :Parameters:
  181.             `bus_name` : str
  182.                 A bus name, either unique or well-known
  183.         :Returns: a `dbus.UInt32`
  184.         :Since: 0.80.0
  185.         '''
  186.         validate_bus_name(bus_name)
  187.         return self.call_blocking(BUS_DAEMON_NAME, BUS_DAEMON_PATH, BUS_DAEMON_IFACE, 'GetConnectionUnixUser', 's', (bus_name,))
  188.  
  189.     
  190.     def start_service_by_name(self, bus_name, flags = 0):
  191.         '''Start a service which will implement the given bus name on this Bus.
  192.  
  193.         :Parameters:
  194.             `bus_name` : str
  195.                 The well-known bus name to be activated.
  196.             `flags` : dbus.UInt32
  197.                 Flags to pass to StartServiceByName (currently none are
  198.                 defined)
  199.  
  200.         :Returns: A tuple of 2 elements. The first is always True, the
  201.             second is either START_REPLY_SUCCESS or
  202.             START_REPLY_ALREADY_RUNNING.
  203.  
  204.         :Raises `DBusException`: if the service could not be started.
  205.         :Since: 0.80.0
  206.         '''
  207.         validate_bus_name(bus_name)
  208.         return (True, self.call_blocking(BUS_DAEMON_NAME, BUS_DAEMON_PATH, BUS_DAEMON_IFACE, 'StartServiceByName', 'su', (bus_name, flags)))
  209.  
  210.     
  211.     def request_name(self, name, flags = 0):
  212.         '''Request a bus name.
  213.  
  214.         :Parameters:
  215.             `name` : str
  216.                 The well-known name to be requested
  217.             `flags` : dbus.UInt32
  218.                 A bitwise-OR of 0 or more of the flags
  219.                 `NAME_FLAG_ALLOW_REPLACEMENT`,
  220.                 `NAME_FLAG_REPLACE_EXISTING`
  221.                 and `NAME_FLAG_DO_NOT_QUEUE`
  222.         :Returns: `REQUEST_NAME_REPLY_PRIMARY_OWNER`,
  223.             `REQUEST_NAME_REPLY_IN_QUEUE`,
  224.             `REQUEST_NAME_REPLY_EXISTS` or
  225.             `REQUEST_NAME_REPLY_ALREADY_OWNER`
  226.         :Raises `DBusException`: if the bus daemon cannot be contacted or
  227.             returns an error.
  228.         '''
  229.         validate_bus_name(name, allow_unique = False)
  230.         return self.call_blocking(BUS_DAEMON_NAME, BUS_DAEMON_PATH, BUS_DAEMON_IFACE, 'RequestName', 'su', (name, flags))
  231.  
  232.     
  233.     def release_name(self, name):
  234.         '''Release a bus name.
  235.  
  236.         :Parameters:
  237.             `name` : str
  238.                 The well-known name to be released
  239.         :Returns: `RELEASE_NAME_REPLY_RELEASED`,
  240.             `RELEASE_NAME_REPLY_NON_EXISTENT`
  241.             or `RELEASE_NAME_REPLY_NOT_OWNER`
  242.         :Raises `DBusException`: if the bus daemon cannot be contacted or
  243.             returns an error.
  244.         '''
  245.         validate_bus_name(name, allow_unique = False)
  246.         return self.call_blocking(BUS_DAEMON_NAME, BUS_DAEMON_PATH, BUS_DAEMON_IFACE, 'ReleaseName', 's', (name,))
  247.  
  248.     
  249.     def list_names(self):
  250.         '''Return a list of all currently-owned names on the bus.
  251.  
  252.         :Returns: a dbus.Array of dbus.UTF8String
  253.         :Since: 0.81.0
  254.         '''
  255.         return self.call_blocking(BUS_DAEMON_NAME, BUS_DAEMON_PATH, BUS_DAEMON_IFACE, 'ListNames', '', (), utf8_strings = True)
  256.  
  257.     
  258.     def list_activatable_names(self):
  259.         '''Return a list of all names that can be activated on the bus.
  260.  
  261.         :Returns: a dbus.Array of dbus.UTF8String
  262.         :Since: 0.81.0
  263.         '''
  264.         return self.call_blocking(BUS_DAEMON_NAME, BUS_DAEMON_PATH, BUS_DAEMON_IFACE, 'ListNames', '', (), utf8_strings = True)
  265.  
  266.     
  267.     def get_name_owner(self, bus_name):
  268.         '''Return the unique connection name of the primary owner of the
  269.         given name.
  270.  
  271.         :Raises `DBusException`: if the `bus_name` has no owner
  272.         :Since: 0.81.0
  273.         '''
  274.         validate_bus_name(bus_name, allow_unique = False)
  275.         return self.call_blocking(BUS_DAEMON_NAME, BUS_DAEMON_PATH, BUS_DAEMON_IFACE, 'GetNameOwner', 's', (bus_name,), utf8_strings = True)
  276.  
  277.     
  278.     def watch_name_owner(self, bus_name, callback):
  279.         '''Watch the unique connection name of the primary owner of the
  280.         given name.
  281.  
  282.         `callback` will be called with one argument, which is either the
  283.         unique connection name, or the empty string (meaning the name is
  284.         not owned).
  285.  
  286.         :Since: 0.81.0
  287.         '''
  288.         return NameOwnerWatch(self, bus_name, callback)
  289.  
  290.     
  291.     def name_has_owner(self, bus_name):
  292.         '''Return True iff the given bus name has an owner on this bus.
  293.  
  294.         :Parameters:
  295.             `bus_name` : str
  296.                 The bus name to look up
  297.         :Returns: a `bool`
  298.         '''
  299.         return bool(self.call_blocking(BUS_DAEMON_NAME, BUS_DAEMON_PATH, BUS_DAEMON_IFACE, 'NameHasOwner', 's', (bus_name,)))
  300.  
  301.     
  302.     def add_match_string(self, rule):
  303.         '''Arrange for this application to receive messages on the bus that
  304.         match the given rule. This version will block.
  305.  
  306.         :Parameters:
  307.             `rule` : str
  308.                 The match rule
  309.         :Raises `DBusException`: on error.
  310.         :Since: 0.80.0
  311.         '''
  312.         self.call_blocking(BUS_DAEMON_NAME, BUS_DAEMON_PATH, BUS_DAEMON_IFACE, 'AddMatch', 's', (rule,))
  313.  
  314.     
  315.     def add_match_string_non_blocking(self, rule):
  316.         '''Arrange for this application to receive messages on the bus that
  317.         match the given rule. This version will not block, but any errors
  318.         will be ignored.
  319.  
  320.  
  321.         :Parameters:
  322.             `rule` : str
  323.                 The match rule
  324.         :Raises `DBusException`: on error.
  325.         :Since: 0.80.0
  326.         '''
  327.         self.call_async(BUS_DAEMON_NAME, BUS_DAEMON_PATH, BUS_DAEMON_IFACE, 'AddMatch', 's', (rule,), None, None)
  328.  
  329.     
  330.     def remove_match_string(self, rule):
  331.         '''Arrange for this application to receive messages on the bus that
  332.         match the given rule. This version will block.
  333.  
  334.         :Parameters:
  335.             `rule` : str
  336.                 The match rule
  337.         :Raises `DBusException`: on error.
  338.         :Since: 0.80.0
  339.         '''
  340.         self.call_blocking(BUS_DAEMON_NAME, BUS_DAEMON_PATH, BUS_DAEMON_IFACE, 'RemoveMatch', 's', (rule,))
  341.  
  342.     
  343.     def remove_match_string_non_blocking(self, rule):
  344.         '''Arrange for this application to receive messages on the bus that
  345.         match the given rule. This version will not block, but any errors
  346.         will be ignored.
  347.  
  348.  
  349.         :Parameters:
  350.             `rule` : str
  351.                 The match rule
  352.         :Raises `DBusException`: on error.
  353.         :Since: 0.80.0
  354.         '''
  355.         self.call_async(BUS_DAEMON_NAME, BUS_DAEMON_PATH, BUS_DAEMON_IFACE, 'RemoveMatch', 's', (rule,), None, None)
  356.  
  357.  
  358.